package org.dong.Controller;

import com.alibaba.fastjson.JSON;
import org.dong.Dao.FileDaoImpl;
import org.dong.Pojo.Dto;
import org.dong.Pojo.HttpRequest;
import org.dong.Pojo.HttpResponse;
import org.dong.Tool.Data;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class UpdateNameController implements ServerLet {

    @Override
    public void server(HttpRequest request, OutputStream os) {
        Dto dto = new Dto();
        String msg = "ok";
        //查找数据中是否存在此记录

        String[] list = request.getRequestData().split("&");
        //获取文件ID
        String file_name = URLDecoder.decode(list[1].split("=")[1]);
        int id = Integer.parseInt(list[0].split("=")[1]);
        Map<String,String> map = new HashMap<>();
        map.put("file_name",file_name);
        map.put("id",id+"");
        if(new FileDaoImpl().updateName(map)){
            dto.setErrMsg("更新成功");
            dto.setStatusCode(2000);
        }else{
            dto.setStatusCode(2001);
            dto.setErrMsg("更新失败");
        }

        msg = JSON.toJSONString(dto);

        new HttpResponse(os,request.getUrl(),msg);
    }
}
